import csv
import json


def convert_csv_to_json(csv_file_path):
    # 读取CSV文件
    with open(csv_file_path, 'r', encoding='utf-8') as file:
        reader = csv.DictReader(file)
        data = list(reader)
    # 转换数据格式
    converted_data = []
    for row in data:
        # 确保问题和答案都不为空
        if row['question_text'] and row['answer_text']:
            # 创建一组对话，包含用户问题和助手回答
            conversation = [{"role": "user","content": row['question_text']},{"role": "assistant","content": row['answer_text']}]
            # 将对话组添加到结果列表中
            converted_data.append(conversation)
    return converted_data


# 使用示例
csv_file_path = 'D:\\yuanbei\\客服问答机器人需求表\\清洗数据\\30056-TOPSLOT&Ablepay🇵🇰EP6.0-JC4.5+2+ChatExport_2025-10-03\\qa_pairs.csv'  # 替换为你的CSV文件路径
json_data = convert_csv_to_json(csv_file_path)

# 保存为JSON文件
with open('D:\\yuanbei\\客服问答机器人需求表\\清洗数据\\30056-TOPSLOT&Ablepay🇵🇰EP6.0-JC4.5+2+ChatExport_2025-10-03\\train_data1.json', 'w', encoding='utf-8') as json_file:
    json.dump(json_data, json_file, ensure_ascii=False, indent=2)

print("转换完成，数据已保存为 train_data1.json")